#include "Pickup Item.h"
#include "GameCommander.h"

Pickup::Pickup() : BaseObject()
{
	Init();
	m_vPosition = D3DXVECTOR3(0,0,0);
	m_PUType = PU_NO_TYPE;
	m_Mesh = Pen::Engine()->getMeshByIndex(COIN_MESH);
	m_pTexture = Pen::Engine()->getTextureByIndex(GREEN_TEXTURE);
	SetBoundingBox(1);
}
Pickup::Pickup(D3DXVECTOR3 Pos,ePickUpType Type,eMeshs Mesh,eTextures Texture) : BaseObject()
{
	Init();
	m_vPosition = Pos;
	m_PUType = Type;
	m_Mesh = Pen::Engine()->getMeshByIndex(Mesh);
	m_pTexture = Pen::Engine()->getTextureByIndex(Texture);
	SetBoundingBox(1);
}
Pickup::Pickup(D3DXVECTOR3 Pos,ePickUpType Type,eMeshs Mesh,eTextures Texture,int BoundingMod) : BaseObject()
{
	Init();
	m_vPosition = Pos;
	m_PUType = Type;
	m_Mesh = Pen::Engine()->getMeshByIndex(Mesh);
	m_pTexture = Pen::Engine()->getTextureByIndex(Texture);
	SetBoundingBox(BoundingMod);
}

void Pickup::Init()
{
	Found = false;
	Rotating = 0;
	m_Type	= ITEM_TYPE;
}

void Pickup::SetBoundingBox(int Mod)
{
	m_BoundingBox.top		= (LONG)(m_vPosition.y + 1 * Mod);
	m_BoundingBox.left		= (LONG)(m_vPosition.x + 1 * Mod);
	m_BoundingBox.bottom	= (LONG)(m_vPosition.y - 1 * Mod);
	m_BoundingBox.right		= (LONG)(m_vPosition.x - 1 * Mod);
}

//Receives "Found" message, which prompts it to send the give message if its a piece of gold
// or set the won state if its the end marker
void Pickup::TranslateMessages()
{
	Messenger::Engine()->ReceiveMessages(this);
	if(m_Messages.getSize() != 0)
	{
		for(int i = 0;i < m_Messages.getSize();++i)
		{
			switch(m_Messages.get(i)->MessageType)
			{
			case FOUND:
				Found = true;
				if(m_PUType == END_MARKER_TYPE)
				{
					GAME::Engine()->WON(0);
				}
				if(m_PUType == GOLD_TYPE)
				{
					Messenger::Engine()->SendMessageA(GIVE,1,GAME::Engine()->Player()->m_iID);
				}
				Messenger::Engine()->ClearMessage(m_Messages.get(i));
			default:
				break;
			}
		}
		m_Messages.Clear();
	}
}

//All items rotate based off the data passed in which is time
void* Pickup::Update(void* Data)
{
	Rotating += .1f;
	if(Rotating >= 360)
		Rotating = 0;
	m_stRotation.roll = Rotating;
	TranslateMessages();
	return NULL;
}